//
//  DDRegisterVC.m
//  dingdang
//
//  Created by Jiangxp on 15/6/29.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDRegisterVC.h"
#import "GlobalDefine.h"
#import "DDNetWorkAPIClient.h"
#import "StringUtil.h"
#import "UICommonFunc.h"
#import "DDUserAgreementVC.h"

@interface DDRegisterVC ()

@end

@implementation DDRegisterVC

-(void)createUI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"注  册", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    
    [self autoTFHeight:_phoneNum];
    [self autoTFHeight:_phoneCode];
    [self autoTFHeight:_nickName];
    [self autoTFHeight:_password];
    
    self.phoneCodeBtn.layer.cornerRadius = 10;
    self.phoneCodeBtn.backgroundColor = [UIColor whiteColor];
    self.phoneCodeBtn.layer.borderColor = RGB(27, 142, 228).CGColor;
    self.phoneCodeBtn.layer.borderWidth = 1.0;
    
    self.regsiterButton.layer.cornerRadius = 10;
}

- (void)autoTFHeight:(UITextField *)tf
{
    CGRect rect = tf.frame;
    rect.size.height = 50;
    tf.frame = rect;
}

-(void)createData
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self AddLeftBtnAction:@"" normal:@"top_返回" selected:@"" action:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];

    self.agreenBtn.selected = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)getPhoneCode:(id)sender {
    if (![StringUtil checkPhoneNumInput:_phoneNum.text ]) {
        [UICommonFunc showAlert:@"请输入11位手机号！"];
    }
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network userPhoneCode:_phoneNum.text completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
        if (isSuccess) {
            [self.phoneCodeBtn startTimer:60];
        }else{
            [UICommonFunc showAlert:message];
        }    }];
}

- (IBAction)userRegister:(id)sender
{
    if (![StringUtil checkPhoneNumInput:_phoneNum.text ]) {
        [UICommonFunc showAlert:@"请输入11位手机号！"];
        return;
    }
    
    if (_password.text.length > 14 || _password.text.length < 6) {
        [UICommonFunc showAlert:@"请输入您的密码！"];
        return;
    }
    
    DDSharedUser *user = [DDSharedUser shared];
    [user updateUserDefaults:_phoneNum.text forKey:kUsername];
    [user updateUserDefaults:_password.text forKey:kPassword];
    
    if (_nickName.text.length == 0) {
        [UICommonFunc showAlert:@"请输入昵称！"];
        return;
    }
    
    if (_phoneCode.text.length == 0) {
        [UICommonFunc showAlert:@"请输入验证码！"];
        return;
    }
    if (_agreenBtn.selected != YES) {
        [UICommonFunc showAlert:@"请勾选同意协议！"];
        return;
    }
    
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network userRegister:_phoneNum.text phoneCode:@"" password:_password.text nickName:_nickName.text fid:@"" completionBlock:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status, NSDictionary *result) {
        [UICommonFunc showAlert:message];
        //[self delayHUD:message];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (IBAction)clickBackground:(id)sender {
    [sender endEditing:YES];
}

- (IBAction)agreeBtnClicked:(UIButton *)sender {
    self.agreenBtn.selected = !_agreenBtn.selected;
    if (_agreenBtn.selected == YES) {
        DDUserAgreementVC *userAgreementVC = [[DDUserAgreementVC alloc] init];
        [self.navigationController pushViewController:userAgreementVC animated:YES];
    }
}




@end
